<?php
require_once (MODEL_PATH . "DAO/connection_mysql.php");
require_once (MODEL_PATH . "DAO/dao_interface.php");

class Artist implements DAOInterface {
	private $id;
	private $name;
	private $tags;
		
	public function getId(){
		return $this->id;
	}
	
	public function setId($artistId) {
		$this->id = $artistId;
	}
	
	public function getName() {
		return $this->name;
	}
	
	public function setName($artistName) {
		$this->name = $artistName;
	}
	
	public function getTags() {
		return $this->tags;
	}
	
	public function setTags($artistTags) {
		$this->tags = $artistTags;
	}	
	
	public function __construct() {
		$this->id = 0;
		$this->name = "";
		$this->tags = "";
	}	
	
	public function add() {		
		$conn = new ConnectionMySQL();
		$conn->connect();		
		
		$sqlFormat = "
			INSERT INTO artist (name, tags)
			VALUES ('%s', '%s');";
		
		//$name = htmlentities($this->getName(), ENT_QUOTES, "UTF-8");
		//$tags = htmlentities($this->getTags(), ENT_QUOTES, "UTF-8");
		
		$query = sprintf($sqlFormat, 
				$conn->escape_string($this->getName()),
				$conn->escape_string($this->getTags()));
		
		$conn->query ( $query );		
		$this->setId ($conn->last_insert_id());
		
		$conn->disconnect();
	}
	
	public function delete(){
		
	}
	
	public function update(){
		
	}
	
	public function getAll() {
		
	}
	
	public function isExist() {
		$conn = new ConnectionMySQL();
		$conn->connect();
		
		$sqlFormat = "SELECT id FROM artist WHERE name='%s';";
		$query = sprintf($sqlFormat, 
				$conn->escape_string($this->getName()));
		
		$result = $conn->query($query);		
		$artistId = $conn->get_value_returned($result);		
		
		$conn->disconnect();
				
		return $artistId;
	}
	
	public static function getArtistById($id) {
		$conn = new ConnectionMySQL();
		$conn->connect();
		
		$sqlFormat = "SELECT * FROM artist WHERE id=%d;";
		$query = sprintf($sqlFormat, $id);
		
		$result = $conn->query($query);		
		$obj = $conn->get_object_returned($result);
		
		$artist = new Artist();
		$artist->setId($obj["id"]);
		$artist->setName($obj["name"]);
		$artist->setTags($obj["tags"]);
		
		$conn->disconnect();
		
		return $artist;
	}
	
}
?>